package net.toocruel.iqismart.web.controller.home;

import net.toocruel.iqismart.entity.common.Tag;
import net.toocruel.iqismart.entity.core.*;
import net.toocruel.iqismart.entity.rbac.User;
import net.toocruel.iqismart.entity.setting.Setting;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.*;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/14 下午2:12
 */
@Controller
public class HomeController extends AbstractBaseController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;


    @Autowired
    private RecommendationService recommendationService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private AuthenticationService authenticationService;
    @Autowired
    private NoticeService noticeService;

    @RequestMapping("")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView(getTemplate("/index"));

        List<Recommendation> latestRecommendations = recommendationService.getLatestRecommendations(13);
        List<Question> latestQuestions = questionService.getLatestQuestions(8);
        List<Question> latestRewardQuestion = questionService.getLatestRewardQuestions(8);
        List<Article> hotArticles = articleService.getHotArticles(8);
        List<Article> latestArticles = articleService.getLatestArticles(8);
        List<Authentication>  recommendAuthentication =  authenticationService.getRecommendAuthentication(8);
        List<Notice> latestNotices = noticeService.getLatestNotices(5);
        List<Tag> hotTags = tagService.getHotTags(20);
        List<User> mostCoinUsers = userService.getMostCoinUsers(8);

        modelAndView.addObject("latestRecommendations",latestRecommendations);
        modelAndView.addObject("latestQuestions",latestQuestions);
        modelAndView.addObject("latestRewardQuestion",latestRewardQuestion);
        modelAndView.addObject("hotArticles",hotArticles);
        modelAndView.addObject("latestArticles",latestArticles);
        modelAndView.addObject("recommendAuthentication",recommendAuthentication);
        modelAndView.addObject("latestNotices",latestNotices);
        modelAndView.addObject("hotTags",hotTags);
        modelAndView.addObject("mostCoinUsers",mostCoinUsers);

        //seoTitle
        String seoTitle = Setting.SETTING.getSeo().getIndexTitle();
        seoTitle = seoTitle.replace("{wzmc}",Setting.SETTING.getWebsite().getName());
        seoTitle = seoTitle.replace("{wzkh}",Setting.SETTING.getWebsite().getSlogan());

        modelAndView.addObject("seoTitle",seoTitle);

        return modelAndView;
    }



    @Override
    protected String getModule() {
        return "home";
    }

}
